package com.freemyleft.left.zapp.net.interceptors;

import java.io.IOException;
import java.util.LinkedHashMap;

import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by FreeMyleft on 2017/12/4.
 */

public class BaseInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        return null;
    }

    /**
     * 获得一个有序排列的url参数对
     * @param chain okhttp3特有的
     * @return
     */
    protected LinkedHashMap<String, String> getUrlParameters(Chain chain) {
        final HttpUrl url = chain.request().url();
        int size = url.querySize();
        final LinkedHashMap<String, String> params = new LinkedHashMap<>();
        for (int i = 0; i < size; i++) {
            params.put(url.queryParameterName(i), url.queryParameterValue(i));
        }
        return params;
    }

    /**
     * 通过key值获取value
     * @param chain
     * @param key
     * @return
     */
    protected String getUrlParameters(Chain chain,String key){
        final Request request=chain.request();
        return request.url().queryParameter(key);

    }
    /**
     * 从POst请求体中获取参数的方法
     */
    protected LinkedHashMap<String ,String >getBodyPArameters(Chain chain){
        final FormBody formBody= (FormBody) chain.request().body();
        final LinkedHashMap<String ,String >params=new LinkedHashMap<>();
        int size=formBody.size();
        for (int i=0;i<size;i++){
            params.put(formBody.name(i),formBody.value(i));
        }
        return params;


    }
    protected String  getBodyParameters(Chain chain,String key){
        return getUrlParameters(chain).get(key);
    }
}
